package install

import (
	"kubeInstall/pkg/client/ssh"
	"kubeInstall/pkg/logger"
	"kubeInstall/server/models/cluster"
)

type SShOptions struct {
	User     string
	Password string
	HostIp   string
	Port     int
	PkFile   string
}

func NewSshOptions(node cluster.Host) *SShOptions {
	sshOpt := &SShOptions{
		User:   node.Credential.Username,
		HostIp: node.IP,
		Port:   node.Port,
	}

	if node.Credential.Type == "password" {
		sshOpt.Password = node.Credential.CredentialData
	} else {
		sshOpt.PkFile = node.Credential.CredentialData
	}
	return sshOpt
}
type SShClient struct {
}

func NewSShClient(opt *SShOptions) (*ssh.SSH, error) {
	sshClient := &ssh.SSH{
		User:     opt.User,
		Password: opt.Password,
		HostIp:   opt.HostIp,
		Port:     opt.Port,
		PkFile:   opt.PkFile,
	}
	err := sshClient.SshInit()
	if err != nil {
		logger.Error("[ssh][%s]Error create ssh session failed,%s", opt.HostIp, err)
		return nil, err
	}

	return sshClient, nil
}
